﻿/// <reference path="//Microsoft.WinJS.1.0/js/base.js" />

(function () {

    var md = new Array(12);
    md[0] = 31;
    md[1] = 28 + md[0];
    md[2] = 31 + md[1];
    md[3] = 30 + md[2];
    md[4] = 31 + md[3];
    md[5] = 30 + md[4];
    md[6] = 31 + md[5];
    md[7] = 31 + md[6];
    md[8] = 30 + md[7];
    md[9] = 31 + md[8];
    md[10] = 30 + md[9];
    md[11] = 31 + md[10];

    var MyDate = WinJS.Class.define(
        function (year, month, date) {
            this.year = year;
            this.month = month;
            this.date = date;

            this.getValue = function () {
                var that = this;

                if (!that._value) {
                    var sum = 0;
                    sum += this.year * md[11];
                    sum += Math.floor(this.year - 1 / 4);
                    if (this.year % 4 == 0 && this.month > 1) {
                        sum++;
                    }
                    if (this.month != 0) sum += md[month - 1];
                    sum += this.date;

                    that._value = sum;
                }

                return that._value;
            }

            this.toString = function () {
                return (this.month + 1) + "/" + (this.date + 1) + "/" + this.year;
            }
        }
    );

    var AssetPriceItem = WinJS.Class.define(
        function (values, symbol) {
            var split = values[0].split("-");
            this.date = new MyDate(new Number(split[0]), new Number(split[1])-1, new Number(split[2]));
            this.symbol = symbol;
            this.openPrice = parseFloat(values[1]);
            this.highPrice = parseFloat(values[2]);
            this.lowPrice = parseFloat(values[3]);
            this.closePrice = parseFloat(values[4]);
            this.volume = parseFloat(values[5]);
            this.adjustedClosePrice = parseFloat(values[6]);
        }
    );

    var PortfolioInfo = WinJS.Class.define(
        function (name, startDate, assets) {
            this.name = name;
            var parsedDate = new Date(startDate);
            this.date = startDate;
            this.assets = assets;
        }
    );

    var AssetInfo = WinJS.Class.define(
        function (symbol, prices) {
            this.symbol = symbol;
            this.prices = prices;
        }
    );

    var DailyRateOfReturnInfo = WinJS.Class.define(
        function (first, second, simple) {
            this.firstAssetInfo = first;
            this.secondAssetInfo = second;
            if (simple) {
                this.rate = (this.secondAssetInfo.adjustedClosePrice - this.firstAssetInfo.adjustedClosePrice) / this.firstAssetInfo.adjustedClosePrice;
            } else {
                this.rate = Math.log(this.secondAssetInfo.adjustedClosePrice / this.firstAssetInfo.adjustedClosePrice);
            }
        }
    );

    WinJS.Namespace.define("FolioFixer.DataTypes", {
        AssetPriceItem: AssetPriceItem,
        PortfolioInfo: PortfolioInfo,
        AssetInfo: AssetInfo,
        DailyRateOfReturnInfo: DailyRateOfReturnInfo,
        Date: MyDate
    });
})();